#!/bin/bash

APPS=("worldcup-back")
REMOTES=("gmj@118.31.32.197")

VERSION=1.0.0

REMOTE_PATH="/home/gmj/code/worldcup-back"
ROOT=$(cd `dirname $0`; pwd)
echo "ROOT = " + $ROOT

echo "------------ 本地路径为 $ROOT ------------"

echo "请选择发布环境 dev publish ?"
read env
echo "------------ 准备发布 $env ------------"

# maven
cd $ROOT
mvn clean install -Dmaven.test.skip=true -P $env

for APP in ${APPS[@]}
do
  for REMOTE in ${REMOTES[@]}
  do
    APPROOT="${ROOT}/target"
    #解压到APPROOT
    #echo "tar -zxvf $APPROOT/$APP-$VERSION-$env.tar.gz -C $APPROOT/"
    #tar -zxvf $APPROOT/$APP-$VERSION-$env.tar.gz -C $APPROOT/
    echo "------------ 备份 ${REMOTE} ${APP} 开始  ------------"
    timeStr=$(date +%Y%m%d%H%M%S)
    echo "timeStrtimeStrtimeStr: " + ${timeStr}

    ssh $REMOTE "mv $REMOTE_PATH/${APP} $REMOTE_PATH/${APP}.${timeStr}.bak"
    echo "------------ 备份 ${REMOTE} ${APP} 完成 ------------"

    echo "------------ 上传 ${REMOTE} ${APP} 开始 ------------"
    rsync -avzP $APPROOT/$APP-$VERSION-$env.jar $REMOTE:$REMOTE_PATH/$APP/
    echo "------------ 上传 ${REMOTE} ${APP} 成功 ------------"
#    echo "------------ 是否要启动 ${REMOTE} ${APP} 服务 [y/n] ------------"
#    read isRun
#    if [ "$isRun" = "y" -o  "$isRun" = "Y" ]
#    then
#      ssh $REMOTE "sh $REMOTE_PATH/$APP/bin/run.sh /home/work/lsh-payment/payment-provider 1"
#      echo "------------ ${REMOTE} ${APP} 启动成功 ------------"
#    fi
    rm -rf $APPROOT/$APP-$VERSION-$env
  done
done

echo "------------ 执行完毕 ------------"